package com.yaodian100.mall.member.web;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.yaodian100.core.common.utils.ServerValue;

/**
 * 
 * @author apple.chang
 * 
 */
public class SimulateLoginFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		String host = httpRequest.getHeader("X-Forwarded-For");
		if (StringUtils.isBlank(host))
			host = request.getRemoteAddr();
		Pattern pattern = Pattern.compile(ServerValue.getInnerIP());
		Matcher matcher = pattern.matcher(host);
		if (matcher.find())
			chain.doFilter(request, response);
		else
			httpResponse.sendRedirect(ServerValue.getFrontHostname() + ServerValue.getFrontContextPath() + "/tologin.do");
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
}